/**
 * created on 2007-2-8
 *
 * @author keen.jiang
 *
 */
package com.xbang.core.page;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.taglibs.standard.tag.el.core.ExpressionUtil;

import com.xbang.core.utils.FreeMarkerUtils;

/**
 * @author keen.jiang
 * 
 */
public class PaginationTag extends BodyTagSupport {

	private static final Log logger = LogFactory.getLog(PaginationTag.class);

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -9002831087322134048L;

	/**
	 * class name of CSS Style
	 */
	private String styleClass;

	/**
	 * the condition need to met to render the paginatino segament
	 */
	private String test;

	/**
	 * the url to submit
	 */
	private String action;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
	 */
	public int doStartTag() throws JspException {

		// determine from the el expression passed in the tag
		if (StringUtils.isNotBlank(this.getTest())) {
			if ((Boolean) ExpressionUtil.evalNotNull("page", "test", test, java.lang.Boolean.class, this, pageContext))
				outputPaginationPart();
		} else
			outputPaginationPart();

		return EVAL_BODY_INCLUDE;
	}

	/**
	 * generate pagination html fragment
	 * 
	 * @throws JspException
	 * 
	 */
	private void outputPaginationPart() throws JspException {

		Page page = (Page) pageContext.getRequest().getAttribute("page");

		JspWriter writer = pageContext.getOut();

		try {
			String result = processPaginationTemplate(page);
			writer.write(result);

		} catch (IOException e) {
			logger.error(e);
		}
	}

	@SuppressWarnings("unchecked")
	private String processPaginationTemplate(Page page) throws JspException {

		Map root = new HashMap();

		root.put("styleClass", this.getStyleClass());
		root.put("action", ExpressionUtil.evalNotNull("page", "action", this.getAction(), String.class, this, pageContext));
		root.put("prePage", page.getPrePage());
		root.put("nextPage", page.getNextPage());
		root.put("totalPage", page.getTotalPage());
		root.put("currentPage", page.getCurrentPage());
		root.put("pageCheckIds", page.getSelectCheckIds() == null ? "" : page.getSelectCheckIds());
		root.put("actionURI", page.getActionURI()==null?"url" : page.getActionURI());
		root.put("formid", new Date().getTime());

		return FreeMarkerUtils.processTemplateFile(root, "pagination.ftl");
	}

	/**
	 * @return Returns the styleClass.
	 */
	public String getStyleClass() {

		return this.styleClass;
	}

	/**
	 * @param styleClass
	 *            The styleClass to set.
	 */
	public void setStyleClass(String styleClass) {

		this.styleClass = styleClass;
	}

	/**
	 * @return Returns the action.
	 */
	public String getAction() {

		return this.action;
	}

	/**
	 * @param action
	 *            The action to set.
	 */
	public void setAction(String action) {

		this.action = action;
	}

	/**
	 * @return Returns the test.
	 */
	public String getTest() {

		return this.test;
	}

	/**
	 * @param test
	 *            The test to set.
	 */
	public void setTest(String test) {

		this.test = test;
	}

}
